/*
 * jQuery Plugin : Frame Animate v1.0
 * http://www.oglen.net/demo/jFrameAnimate/
 *【jquery 逐帧动画插件】
 * Copyright 2013, Justin Fang
 *
 * Date: 2013-1-29
 *
 */
 
(function($){
	$.fn.frameAnimate = function(init, count){
		
		var init = $.extend({
			initX: 0,
			initY: 0,
			x: 0,
			y: 0,
			intervalX: 0,
			intervalY: 0,
			count: 0,
			speed: 100,
			total: 1,
			sync: true
		}, init);

		return this.each(function(){
			
			var $this = $(this);
			var _this = this;

			this.x = init.x;
			this.y = init.y;
			this.speed = init.speed;
			this.total = init.total;
			this.count = init.count;
			this.play = true;

			var setFrame = function(){
				_this.x = init.initX - init.intervalX * (_this.count % _this.total);
				_this.y = init.initY - init.intervalY * (_this.count % _this.total);

				$this.css('background-position', _this.x + 'px ' + _this.y + 'px');
				
				if(init.sync) clearTimeout(_this.play);
				if(typeof count != 'undefined') count(_this.count, _this.total);
				if(_this.play) _this.play = setTimeout(setFrame, init.speed);

				_this.count++;
			}
			setFrame();
		});
	}

	$.fn.stopFrameAnimate = function(){
		return this.each(function(){
			this.play = false;
			clearTimeout(this.play);
		});
	}

})(jQuery);